package com.zl.sanova
{
	import com.zl.anova.HeHfAnova;
	import com.zl.bean.EBean;
	import com.zl.bean.FactorBean;
	import com.zl.bean.HBean;
	import com.zl.bean.TBean;
	import com.zl.enums.FactorType;
	import com.zl.enums.HFactorType;
	
	import common.CommonData;
	
	import flash.utils.Dictionary;
	
	import main.mainView;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.Alert;
	
	import step.step3;
	
	//计算HE、HF的单因素方差
	public class HeHfSanova
	{
		[Bindable] public var factorType:FactorType;
		[Bindable] public var heDataList:ArrayCollection = new ArrayCollection(); 
		
		[Bindable] public var strList:ArrayCollection = new ArrayCollection();
		[Bindable] public var notPassList:ArrayCollection = new ArrayCollection();
		[Bindable] public var dataList:ArrayCollection = new ArrayCollection();
		
		[Bindable] public var F:Number = 0;
		[Bindable] public var FA:Number = 0;
		
		private var resultStr:String = "";
		private var currentAttr:String;
		private var level:int;
		private var tArray:Array = [];
		private var sumArray:Array = [];
		private var groupListArray:Array = [];
		private var groupValueArray:Array = [];
		
		public function init(heDataList:ArrayCollection,cbDict:Dictionary, level:int):void{
			factorType = CommonData.getInstance().factorType;
			this.heDataList = heDataList;
			this.level = level;
			doCalculateSanova(cbDict, level);
		}
		
		private function doCalculateSanova(cbDict:Dictionary, level:int):void{
			currentAttr = CommonData.getInstance().treeType;
			if(currentAttr == ""){
				currentAttr = "he";
			}
			/*if(currentAttr=="he" || currentAttr=="hf"){
				calculateAve(HFactorType.h1, heDataList);
				calculateAve(HFactorType.h2, heDataList);
				calculateAve(HFactorType.h3, heDataList);
			}*/
			onAnalysisL(level, heDataList, cbDict);
		}
		
		public function onAnalysisL(level:int, heDataList:ArrayCollection, cbDict:Dictionary):void{
			CommonData.getInstance().dataList.removeAll();
			notPassList.removeAll();
			for each(var fBean:FactorBean in CommonData.getInstance().factorList){
				if(!cbDict[fBean.id].selected){
					continue;
				}
				
				tArray = new Array();
				sumArray = new Array();
				groupListArray = new Array();
				groupValueArray = new Array();
				
				//var h:FactorType = factorType.getByAttr(fBean.id);
				var h:FactorType = new FactorType();
				
				h._attr = fBean.id;
				h._name = fBean.label;
				/*if(fBean.id.indexOf("@@##") != -1){
					calculateAve1(h, heDataList);
					h._attr = "attribute";
				}*/
				var min:Number = calculateMin(h, heDataList, fBean.id);
				var max:Number = calculateMax(h, heDataList, fBean.id);
				
				//求分组点，min,ha,max
				for(var i:int = 0; i <= level; i++){
					var groupValue:Number = 0;
					if(i == 0){		
						groupValue = min;	//计算最小值  即：groupValueArray[0] = min
					}else if(i == level){	
						groupValue = max;	//计算最大值 即：groupValueArray[1] = max
					}else{
						groupValue = groupValueArray[i-1] + ((max-min)/level);
					}
						groupValueArray.push(groupValue);
				}
				
				//处理分组
				for(var j:int = 0; j < groupValueArray.length-1; j++){
					var groupList:ArrayCollection = new ArrayCollection();
					for each(var item:Object in heDataList){
						/*if(h.attr == "attribute"){
							if(j == 0){
								if(item["attribute"][fBean.id] >= groupValueArray[j] && item["attribute"][fBean.id] <= groupValueArray[j+1]){
									groupList.addItem(item[currentAttr]);
								}
							}else{
								if(item["attribute"][fBean.id] > groupValueArray[j] && item["attribute"][fBean.id] <= groupValueArray[j+1]){
									groupList.addItem(item[currentAttr]);
								}
							}
						}else{
							if(j == 0){
								if(item[h.attr] >= groupValueArray[j] && item[h.attr] <= groupValueArray[j+1]){
									groupList.addItem(item[currentAttr]);
								}
							}else{
								if(item[h.attr] > groupValueArray[j] && item[h.attr] <= groupValueArray[j+1]){
									groupList.addItem(item[currentAttr]);
								}
							}
						}*/
						if(j == 0){
							if(item[h.attr] >= groupValueArray[j] && item[h.attr] <= groupValueArray[j+1]){
								groupList.addItem(item[currentAttr]);
							}
						}else{
							if(item[h.attr] > groupValueArray[j] && item[h.attr] <= groupValueArray[j+1]){
								groupList.addItem(item[currentAttr]);
							}
						}
					}
					calculateTAndSum(groupList);
					groupListArray.push(groupList);
				}
				
				fBean.hMax = max;
				fBean.hMin = min;
				onAnalysisLevel(h.name,level,fBean);
				fBean.hMid1 = groupValueArray[1];
				fBean.hMid2 = groupValueArray[2];
				fBean.hMid3 = groupValueArray[3];
				fBean.pass = true;
			}
			CommonData.getInstance().dataList = dataList;
			CommonData.getInstance().showNotPassList = new ArrayCollection();
			
			var sort:Sort = new Sort();
			sort.fields = [new SortField("FAVal")];
			sort.reverse();
			notPassList.sort = sort;
			notPassList.refresh();
			
			if(notPassList.length >= 1 && notPassList.length <= 4){
				//取前两项影响因素
				for(var k:int = 0; k < notPassList.length; k++ ){
					CommonData.getInstance().showNotPassList.addItem(notPassList.getItemAt(k));
				}
			}else{
				//取前4项影响因素
				for(var l:int = 0; l < notPassList.length; l++ ){
					if(l <= 3){
						CommonData.getInstance().showNotPassList.addItem(notPassList.getItemAt(l));
					}else{
						break;
					}
				}
			}
			
			resultStr = "";
			strList.removeAll();
			
			for each(var hBean:HBean in CommonData.getInstance().showNotPassList){
				hBean.fBean.pass = false;
				resultStr += hBean.title + "——具有显著影响\n";
				strList.addItem(hBean.title);
			}
			
			CommonData.getInstance().resultStr = resultStr;
		}
		
	/*	public function calculateAve(ft:FactorType, heList:ArrayCollection):void{
			var sum:Number = 0;
			var heListCount:int = heList.length;
			var hColumns:Array = ft.column.split(",");
			var hColumnCount:int = hColumns.length;
			for(var i:int = 0; i < heListCount; i++){
				sum = 0;
				for each(var c:String in hColumns){
					sum += Number(heList.getItemAt(i)[c]);
				}
				heList.getItemAt(i)[ft.attr] = sum/hColumnCount;
			}
		}
		
		public function calculateAve1(ft:FactorType, heList:ArrayCollection):void{
			var sum:Number = 0;
			var heListCount:int = heList.length;
			var hColumns:Array = ft.attr.split("@@##");
			var hColumnCount:int = hColumns.length;
			for(var i:int = 0; i < heListCount; i++){
				sum = 0;
				for each(var c:String in hColumns){
					sum += Number(heList.getItemAt(i)[c]);
				}
				heList.getItemAt(i)["attribute"][ft.attr] = sum/hColumnCount;
			}
		}*/
		
		public function calculateMin(ft:FactorType, heDataList:ArrayCollection, attr:String):Number{
			var min:Number = 0;
			/*if(ft.attr == "attribute"){
				min = Number(heDataList.getItemAt(0)["attribute"][attr]);
				for each(var item:Object in heDataList){
					if(Number(item["attribute"][attr]) <= min){
						min = Number(item["attribute"][attr]);
					}
				}
			}else{
				min = Number(heDataList.getItemAt(0)[ft.attr]);
				for each(var item:Object in heDataList){
					if(Number(item[ft.attr]) <= min){
						min = Number(item[ft.attr]);
					}
				}
			}*/
			min = Number(heDataList.getItemAt(0)[ft.attr]);
			for each(var item:Object in heDataList){
				if(Number(item[ft.attr]) <= min){
					min = Number(item[ft.attr]);
				}
			}
			
			return min;
		}
		
		public function calculateMax(ft:FactorType, heDataList:ArrayCollection, attr:String):Number{
			var max:Number = 0;
			/*if(ft.attr == "attribute"){
				max = Number(heDataList.getItemAt(0)["attribute"][attr]);
				for each(var item:Object in heDataList){
					if(Number(item["attribute"][attr]) >= max){
						max = Number(item["attribute"][attr]);
					}
				}
			}else{
				max = Number(heDataList.getItemAt(0)[ft.attr]);
				for each(var item:Object in heDataList){
					if(Number(item[ft.attr]) >= max){
						max = Number(item[ft.attr]);
					}
				}
			}*/
			max = Number(heDataList.getItemAt(0)[ft.attr]);
			for each(var item:Object in heDataList){
				if(Number(item[ft.attr]) >= max){
					max = Number(item[ft.attr]);
				}
			}
			return max;
		}
		
		private function calculateTAndSum(dataList:ArrayCollection):void{
			var tValue:Number = 0;
			var sumItem:Number = 0;
			if(dataList.length == 0){
				tValue = 0;
			}else{
				for each(var item:Number in dataList){
					if(item){
						tValue += item;
						sumItem += item * item;
					}
				}
			}
			tArray.push(tValue);
			sumArray.push(sumItem);
		}
		
		private function onAnalysisLevel(cc:String, level:int, fBean:FactorBean):void{
			var ST:Number = 0;
			var SA:Number = 0;
			var Se:Number = 0;
			var fA:int = 0;
			var fT:int = 0;
			var fe:int = 0;
			var MSA:Number = 0;
			var MSe:Number = 0;
			
			var aveT1:Number = 0;
			var aveT2:Number = 0;
			var aveT3:Number = 0;
			var aveT4:Number = 0;
			var aveT:Array = [];
			
			var totalSum:Number = 0;
			var totalT:Number = 0;
			var totalM:Number = 0;
			var totalTAndM:Number = 0;
			
			for(var i:int = 0; i < level; i++ ){
				totalSum += sumArray[i];
				totalT += tArray[i];
				totalM += groupListArray[i].length;
				totalTAndM += tArray[i]*tArray[i]/groupListArray[i].length;
				aveT[i]=tArray[i]/groupListArray[i].length;
			}
			
			ST = totalSum - (totalT*totalT)/totalM;
			SA = totalTAndM - (totalT*totalT)/totalM;
			
			Se= ST-SA;
			
			fA = level-1;
			fT = totalM-1;
			fe = fT-fA;
			
			MSA = SA/fA;
			MSe = Se/fe;
			
			FA = MSA/MSe;
			
			if(level == 2){
				if (fe>120){
					F=2.71;//需比较的F值F0.9(1,无穷)
				}
				if (fe>60&&fe<=120){
					//F=2.75;//需比较的F0.9(1,120)
					//F=2.2;
					F=0.6
					
				}
				if (fe>40&&fe<=60){
					F=2.79;//需比较的F值F0.9(1,60)
				}
				if (fe>30&&fe<=40){
					F=2.84;//需比较的F值F0.9(1,40)
				}
				if (fe>20&&fe<=30){
					F=2.97-(fe-20)*0.01;//需比较的F值F0.9(1,40)
				}
				if (fe>15&&fe<=20){
					F=3.07-(fe-15)*0.02;
				}
				if (fe>11&&fe<=14){
					F=3.18-(fe-12)*0.04;
				}
				if (fe==11){
					F=3.23;
				}
				if (fe==10){
					F=3.29;
				}
				if (fe==9){
					F=3.36;
				}
				if (fe==8){
					F=3.46;
				}
				if (fe==7){
					F=3.59;
				}
			}else if(level == 3){
				if ((fe)>120){
					F=2.30;//需比较的F值F0.9(2,无穷)
				}
				if (fe>60&&fe<=120){
					F=2.35;//需比较的F值F0.9(2,120)
				}
				if (fe>40&&fe<=60){
					F=2.39;//需比较的F值F0.9(2,60)
				}
				if (fe>30&&fe<=40){
					F=2.44;//需比较的F值F0.9(2,40)
				}
				if (fe>20&&fe<=28){
					F=2.57-(fe-21)*0.01;//需比较的F值F0.9(2,40)
				}
				if (fe==29){
					F=2.50;
				}
				if (fe==30){
					F=2.49;
				}
				if (fe==20){
					F=2.59;
				}
				if (fe==19){
					F=2.61;
				}
				if (fe==18){
					F=2.62;
				}
				if (fe>=13&&fe<=17){
					F=2.76-(fe-13)*0.03;
				}
				if (fe==12){
					F=2.81;
				}
				if (fe==11){
					F=2.86;
				}
				if (fe==10){
					F=2.92;
				}
				if (fe==9){
					F=3.01;
				}
				if (fe==8){
					F=3.11;
				}
				if (fe==7){
					F=3.26;
				}
			}else if(level == 4){
				if (fe>120){
					F=2.08;//需比较的F值F0.9(3,无穷)
				}
				if (fe>60&&fe<=120){
					F=2.13;//需比较的F值F0.9(1,120)
				}
				if (fe>40&&fe<=60){
					F=2.18;//需比较的F值F0.9(1,60)
				}
				if (fe>30&&fe<=40){
					F=2.23;//需比较的F值F0.9(1,40)
				}
				if (fe>20&&fe<=28){
					F=2.36-(fe-21)*0.01;//需比较的F值F0.9(1,40)
				}
				if (fe==29||fe==30){
					F=2.28;
				}
				if (fe>=16&&fe<=21){
					F=2.46-(fe-16)*0.02;
				}
				if (fe==15){
					F=2.49;
				}
				if (fe==14){
					F=2.52;
				}
				if (fe==13){
					F=2.56;
				}
				if (fe==12){
					F=2.61;
				}
				if (fe==11){
					F=2.66;
				}
				if (fe==10){
					F=2.73;
				}
				if (fe==9){
					F=2.81;
				}
				if (fe==8){
					F=2.92;
				}
				if (fe==7){
					F=3.07;
				}
			}
			var hBean:HBean = new HBean();
			var eBean:EBean = new EBean();
			var tBean:TBean = new TBean();
			
			hBean.title = cc;
			hBean.sNum = SA.toFixed(4);
			hBean.fNum = fA.toString();
			hBean.msNum = MSA.toFixed(4);
			hBean.FANum = FA.toFixed(4);
			hBean.FAVal = FA;
			hBean.fBean = fBean;
			hBean.groupValueArray = groupValueArray;
			hBean.groupListArray = groupListArray;
			hBean.aveT1 = aveT[0];
			hBean.aveT2 = aveT[1];
			hBean.aveT3 = aveT[2];
			hBean.aveT4 = aveT[3];
			
			eBean.title = "e";
			eBean.sNum = Se.toFixed(4);
			eBean.fNum = fe.toString();
			eBean.msNum = MSe.toFixed(4);
			eBean.FANum = "-";
			
			tBean.title = "T";
			tBean.sNum = ST.toFixed(4);
			tBean.fNum = fT.toString();
			tBean.msNum = "-";
			tBean.FANum = F.toFixed(4);
			dataList.addItem(hBean);
			dataList.addItem(eBean);
			dataList.addItem(tBean);
			
			if(FA >= F){
				notPassList.addItem(hBean);
			}
		}
	}
}